using System;
using System.Collections.Generic;
using CMS_Admin.Backend.Api.Entity;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using CMS_Admin.Backend.Api.ParamModel;
using Newtonsoft.Json;
using CMS_Admin.Backend.Api.Repository;
using CMS_Admin.Backend.Api.Utils;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authentication;

namespace CMS_Admin.Backend.Controllers
{
    /// <summary>
    /// 评论管理
    /// </summary>
    [Authorize]
    [ApiController]
    [Route("comment")]
    public class CommentController : ControllerBase
    {
        private IRepository<Users> _usersRespository;
        private IRepository<Articles> _articles;
        private IRepository<Comments> _comments;




        private readonly IConfiguration _configuration;

        public CommentController(IConfiguration configuration, IRepository<Users> usersRespository, IRepository<Articles> articles, IRepository<Comments> comments)
        {
            _configuration = configuration;
            _usersRespository = usersRespository;
            _articles = articles;
            _comments = comments;
        }

        /// <summary>
        /// 获取评论列表
        /// </summary>
        /// <param name="id"></param>
        /// <param name="pager"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet("{id}")]
        public dynamic Get(int id, [FromQuery] Pager pager)
        {

            var pageIndex = pager.PageIndex;
            var pageSize = pager.PageSize;

            var comment = from comments in _comments.Table
                          join articles in _articles.Table on comments.ArticlesId equals articles.Id
                          join user in _usersRespository.Table on comments.UsersId equals user.Id
                          orderby comments.Id
                          where comments.ArticlesId == id
                          select new
                          {
                              Id = comments.Id,
                              UsersId = comments.UsersId,
                              ArticlesId = comments.ArticlesId,
                              Username = user.Username,
                              Nickname =user.Nickname,
                              Txcolor = user.Txcolor,
                              Bgcolor = user.Bgcolor,
                              CreatedTime = comments.CreatedTime,
                              CommentContent = comments.CommentContent,
                            
                          };


            var u = comment.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            var res = new
            {
                Code = 200,
                Data = new { Data = u, Pager = new { pageIndex, pageSize, rowsTotal = comment.Count() } },
                Msg = "获取评论列表成功^_^"
            };
            return res;
        }




        /// <summary>
        /// 插入评论
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public string Post(NewComment model)
        {

            var commentContent = model.CommentContent.Trim();
            // var userId = model.UserId;
            var articlesId = model.ArticlesId;
            var claims = base.HttpContext.AuthenticateAsync().Result
         .Principal.Claims.FirstOrDefault().Subject.Claims;

            var nickname = claims.FirstOrDefault(c => c.Type == "nickname")?.Value;
            var username = claims.FirstOrDefault(c => c.Type == "username")?.Value;
            var userid = claims.FirstOrDefault(c => c.Type == "id")?.Value;

            if (string.IsNullOrEmpty(commentContent))
            {
                var tmp = new
                {
                    Code = 1004,
                    Data = "",
                    Msg = "评论内容不能为空，请确认后重试"
                };
                return JsonHelper.Serialize(tmp);
            }


            var comment = new Comments
            {
                CommentContent = commentContent,
                Users = _usersRespository.Table.Single(r => r.Id == int.Parse(userid)),
                Articles = _articles.Table.Single(r => r.Id == articlesId)

            };

            _comments.Insert(comment);


            var res = new
            {
                Code = 200,
                Data = comment,
                Msg = "评论成功！"
            };
            return JsonHelper.Serialize(res);
        }



    }

}